(function ($) {
	var orderPull;
	var orderPage = 0,Limit = 20;
	var orderPulling = false;
	var orderData;
	var nurseid=""
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	
	//基操--预约
	function getOrderData(){
		if (orderPulling) {
			orderPull.endPullUpToRefresh()
			return;
		}
		orderPulling = true
		var pdict = {'loginID':nurseid,'input':$('#search')[0].value,'parr':'^^1^O^2','start':orderPage,'limit':Limit}
		var paramDict = {'method':'web.INMTeaComm:FindReleaseList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				orderPull.endPullUpToRefresh();
				orderPulling = false
				return;
			}
			if (orderPage == 0) {
				orderData = dataArr;
				$('#orderUL')[0].innerHTML = ''
			}else{
				orderData = orderData.concat(dataArr)
			}
			
			updateOrderUI(dataArr)
			if (dataArr.length < Limit) {
				orderPull.endPullUpToRefresh(true);
			}else{
				orderPull.endPullUpToRefresh();
			}
			orderPage += dataArr.length;
			orderPulling = false
		}, function(errorStr) {
		 	orderPull.endPullUpToRefresh();
		 	orderPulling = false
		});
	}
	function updateOrderUI(dataArr){
		var orderUL = document.getElementById("orderUL");
		var dict = {'L':'科级','H':'院级','W':'病区'}
		var dateTime = new Date().getTime()
		var oldLis = orderUL.querySelectorAll(('li'))
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var level = oneData['ReleaseLevel']
			level = dict[level]
			var timeList = oneData['TimeList'].split('「') //多个批次「
			var html = '<div class="classTitle">'+oneData['ReleaseDesc']+'<div>'+timeList.length+'批次</div></div>'+
				'<div class="classInfo"><div>'+level+'</div><div>'+oneData['Duration']+'分钟</div></div>'
			var outDate=true //已过预约时间
			for (var j = 0; j < timeList.length;j++) {
				var oneTime = timeList[j].split('」') //开始日期」截止日期」预约截止时间」考核时间」考核地点」人数上限
				html +='<div class="classSubInfo">考核日期：'+oneTime[0]+' - '+oneTime[1]+'</div>'
				if (!outDate) { //未过期 可以修改
					continue
				}
				var orderTime = oneTime[2]
				if (orderTime != "") {
					orderTime = new Date(orderTime).getTime()
					if (orderTime >= dateTime) {
						outDate=false
					}
				}
			}
			if (oneData['OrderNum'] == '') {
				if (outDate) { //已过预约时间
					html +='<div class="baseBtn expire">预约</div>'
				}else{
					html += '<div class="baseBtn">预约</div>'
				}
			}else{
				if (outDate) { //已过预约时间
					html +='<div class="baseBtn expire">修改预约</div>'
				}else{
					html +='<div class="baseBtn canChange">修改预约</div>'
				}
			}
			var newLi = document.createElement('li')
			newLi.id = oldLis.length+i + '-li'
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			orderUL.appendChild(newLi)
			var baseBtns = newLi.querySelectorAll('.baseBtn')
			mui.each(baseBtns,function (index,element) {
				element.addEventListener('tap',baseBtnTapped)
			})
		}
	}
	
	function baseBtnTapped(){
		var index = parseInt(this.parentNode.id)
		var data = orderData[index]
		var classStr = this.getAttribute('class')
		if (classStr.indexOf('expire') != -1) {
			mui.toast('已超过预约日期,无法预约')
			return
		}
		var webview = plus.webview.create('toOrder.html', 'toOrder', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{sourceData:data,pageType:'examine'});
		webview.show('fade-in');
		webview.addEventListener('close',resetOrderRefresh)
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var perInfo = app.getState()
		nurseid = perInfo['loginID']
		mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
			if (document.getElementById('search').value != "") {
				document.getElementById('search').value = ""
			}
			resetOrderRefresh()
		});
		var search = document.getElementById('search');
		search.addEventListener('input',function(){
			resetOrderRefresh()
		});
		//预约
		var orderScroll = document.getElementById('orderScroll');
		orderPull = $(orderScroll).pullToRefresh({
			up: {
				callback: function() {
					getOrderData()
				}
			}
		});
		getOrderData();
	}

	function resetOrderRefresh(){
		orderPage = 0;
		orderData = []
		if (orderPull) {
			orderPull.refresh(true)
		}
		getOrderData();
		$('#orderWrapper').scroll().scrollTo(0,0,100);
	}
})(mui);